Java word scramble game [closed]
Posted
by
Dan
on Programmers
See other posts from Programmers
or by Dan
Published on 2011-11-20T16:32:14Z
Indexed on
2011/11/20
18:24 UTC
Read the original article
Hit count: 275
I'm working on code for a word scramble game in Java. I know the code itself is full of bugs right now, but my main focus is getting a vector of strings broken into two separate vectors containing hints and words. The text file that the strings are taken from has a colon separating them. So here is what I have so far.
public WordApp()
{
inputRow = new TextInputBox();
inputRow.setLocation(200,100);
phrases = new Vector <String>(FileUtilities.getStrings());
v_hints = new Vector<String>();
v_words = new Vector<String>();
textBox = new TextBox(200,100);
textBox.setLocation(200,200);
textBox.setText(scrambled + "\n\n Time Left: \t" + seconds/10 + "\n Score: \t" + score);
hintBox = new TextBox(200,200);
hintBox.setLocation(300,400);
hintBox.hide();
Iterator <String> categorize = phrases.iterator();
while(categorize.hasNext())
{
int index = phrases.indexOf(":");
String element = categorize.next();
v_words.add(element.substring(0,index));
v_hints.add(element.substring(index +1));
phrases.remove(index);
System.out.print(index);
}
The FileUtilities file was given to us by the pofessor, here it is.
import java.util.*;
import java.io.*;
public class FileUtilities
{
private static final String FILE_NAME = "javawords.txt";
//------------------ getStrings -------------------------
//
// returns a Vector of Strings
// Each string is of the form: word:hint
// where word contains no spaces.
// The words and hints are read from FILE_NAME
//
//
public static Vector<String> getStrings ( )
{
Vector<String> words = new Vector<String>();
File file = new File( FILE_NAME );
Scanner scanFile;
try
{
scanFile = new Scanner( file);
}
catch ( IOException e)
{
System.err.println( "LineInput Error: " + e.getMessage() );
return null;
}
while ( scanFile.hasNextLine() )
{
// read the word and follow it by a colon
String s = scanFile.nextLine().trim().toUpperCase() + ":";
if( s.length()>1 && scanFile.hasNextLine() )
{
// append the hint and add to collection
s+= scanFile.nextLine().trim();
words.add(s);
}
}
// shuffle
Collections.shuffle(words);
return words;
}
}
© Programmers or respective owner